#Boa:Frame:cadFornFrame

import wx
import wx.grid
from CONTROLE import *

def create(parent):
    return cadFornFrame(parent)

[wxID_CADFORNFRAME, wxID_CADFORNFRAMEALTERARBTN, wxID_CADFORNFRAMECANCELARBTN, 
 wxID_CADFORNFRAMEEXCLUIRBTN, wxID_CADFORNFRAMELIMPARBTN, 
 wxID_CADFORNFRAMELISTBOX1, wxID_CADFORNFRAMENOVOBTN, 
 wxID_CADFORNFRAMEPESQUISABTN, wxID_CADFORNFRAMESALVARBTN, 
 wxID_CADFORNFRAMESTATICBOX1, wxID_CADFORNFRAMESTATICBOX2, 
 wxID_CADFORNFRAMESTATICBOX3, wxID_CADFORNFRAMESTATICTEXT1, 
 wxID_CADFORNFRAMESTATICTEXT2, wxID_CADFORNFRAMESTATICTEXT3, 
 wxID_CADFORNFRAMESTATICTEXT4, wxID_CADFORNFRAMESTATICTEXT5, 
 wxID_CADFORNFRAMESTATICTEXT6, wxID_CADFORNFRAMESTATICTEXT7, 
 wxID_CADFORNFRAMETEXTCTRL1, wxID_CADFORNFRAMETEXTCTRL2, 
 wxID_CADFORNFRAMETEXTCTRL3, wxID_CADFORNFRAMETXTPESQUISA, 
] = [wx.NewId() for _init_ctrls in range(23)]

class cadFornFrame(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_CADFORNFRAME, name=u'cadFornFrame',
              parent=prnt, pos=wx.Point(343, 170), size=wx.Size(566, 489),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Cadastro de Fornecedores')
        self.SetClientSize(wx.Size(550, 451))

        self.staticBox1 = wx.StaticBox(id=wxID_CADFORNFRAMESTATICBOX1,
              label=u'Controle', name=u'staticBox1', parent=self,
              pos=wx.Point(408, 48), size=wx.Size(128, 224), style=0)

        self.staticBox2 = wx.StaticBox(id=wxID_CADFORNFRAMESTATICBOX2,
              label=u'Pesquisa', name='staticBox2', parent=self, pos=wx.Point(8,
              280), size=wx.Size(528, 164), style=0)

        self.staticBox3 = wx.StaticBox(id=wxID_CADFORNFRAMESTATICBOX3,
              label=u'Cadastro de Fornecedores', name='staticBox3', parent=self,
              pos=wx.Point(8, 48), size=wx.Size(384, 224), style=0)

        self.novoBtn = wx.Button(id=wxID_CADFORNFRAMENOVOBTN, label=u'Novo',
              name=u'novoBtn', parent=self, pos=wx.Point(432, 80),
              size=wx.Size(75, 23), style=0)
        self.novoBtn.Bind(wx.EVT_BUTTON, self.OnNovoBtnButton,
              id=wxID_CADFORNFRAMENOVOBTN)

        self.alterarBtn = wx.Button(id=wxID_CADFORNFRAMEALTERARBTN,
              label=u'Alterar', name=u'alterarBtn', parent=self,
              pos=wx.Point(432, 112), size=wx.Size(75, 23), style=0)
        self.alterarBtn.Bind(wx.EVT_BUTTON, self.OnAlterarBtnButton,
              id=wxID_CADFORNFRAMEALTERARBTN)

        self.salvarBtn = wx.Button(id=wxID_CADFORNFRAMESALVARBTN,
              label=u'Salvar', name=u'salvarBtn', parent=self, pos=wx.Point(432,
              144), size=wx.Size(75, 23), style=0)
        self.salvarBtn.Bind(wx.EVT_BUTTON, self.OnSalvarBtnButton,
              id=wxID_CADFORNFRAMESALVARBTN)

        self.cancelarBtn = wx.Button(id=wxID_CADFORNFRAMECANCELARBTN,
              label=u'Cancelar', name=u'cancelarBtn', parent=self,
              pos=wx.Point(432, 176), size=wx.Size(75, 24), style=0)
        self.cancelarBtn.Bind(wx.EVT_BUTTON, self.OnCancelarBtnButton,
              id=wxID_CADFORNFRAMECANCELARBTN)

        self.excluirBtn = wx.Button(id=wxID_CADFORNFRAMEEXCLUIRBTN,
              label=u'Excluir', name=u'excluirBtn', parent=self,
              pos=wx.Point(432, 208), size=wx.Size(75, 23), style=0)
        self.excluirBtn.Bind(wx.EVT_BUTTON, self.OnExcluirBtnButton,
              id=wxID_CADFORNFRAMEEXCLUIRBTN)

        self.staticText1 = wx.StaticText(id=wxID_CADFORNFRAMESTATICTEXT1,
              label=u'Cadastro de Fornecedores', name='staticText1',
              parent=self, pos=wx.Point(160, 8), size=wx.Size(244, 25),
              style=0)
        self.staticText1.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))

        self.pesquisaBtn = wx.Button(id=wxID_CADFORNFRAMEPESQUISABTN,
              label=u'Pesquisa', name=u'pesquisaBtn', parent=self,
              pos=wx.Point(416, 296), size=wx.Size(56, 23), style=0)
        self.pesquisaBtn.Bind(wx.EVT_BUTTON, self.OnPesquisaBtnButton,
              id=wxID_CADFORNFRAMEPESQUISABTN)

        self.limparBtn = wx.Button(id=wxID_CADFORNFRAMELIMPARBTN,
              label=u'Limpar', name=u'limparBtn', parent=self, pos=wx.Point(480,
              296), size=wx.Size(40, 23), style=0)
        self.limparBtn.Bind(wx.EVT_BUTTON, self.OnLimparBtnButton,
              id=wxID_CADFORNFRAMELIMPARBTN)

        self.txtPesquisa = wx.TextCtrl(id=wxID_CADFORNFRAMETXTPESQUISA,
              name=u'txtPesquisa', parent=self, pos=wx.Point(24, 296),
              size=wx.Size(384, 21), style=0, value=u'')

        self.textCtrl1 = wx.TextCtrl(id=wxID_CADFORNFRAMETEXTCTRL1,
              name='textCtrl1', parent=self, pos=wx.Point(72, 96),
              size=wx.Size(100, 21), style=0, value=u'')
        self.textCtrl1.SetEditable(False)
        self.textCtrl1.Enable(False)

        self.textCtrl2 = wx.TextCtrl(id=wxID_CADFORNFRAMETEXTCTRL2,
              name='textCtrl2', parent=self, pos=wx.Point(72, 128),
              size=wx.Size(304, 21), style=0, value=u'')
        self.textCtrl2.SetEditable(False)
        self.textCtrl2.Enable(False)

        self.textCtrl3 = wx.TextCtrl(id=wxID_CADFORNFRAMETEXTCTRL3,
              name='textCtrl3', parent=self, pos=wx.Point(72, 160),
              size=wx.Size(100, 21), style=0, value=u'')
        self.textCtrl3.SetEditable(False)
        self.textCtrl3.Enable(False)

        self.staticText2 = wx.StaticText(id=wxID_CADFORNFRAMESTATICTEXT2,
              label=u'Codigo', name='staticText2', parent=self, pos=wx.Point(24,
              104), size=wx.Size(34, 13), style=0)

        self.staticText3 = wx.StaticText(id=wxID_CADFORNFRAMESTATICTEXT3,
              label=u'Nome', name='staticText3', parent=self, pos=wx.Point(24,
              136), size=wx.Size(28, 13), style=0)

        self.staticText4 = wx.StaticText(id=wxID_CADFORNFRAMESTATICTEXT4,
              label=u'Telefone', name='staticText4', parent=self,
              pos=wx.Point(24, 168), size=wx.Size(43, 13), style=0)

        self.listBox1 = wx.ListBox(choices=[], id=wxID_CADFORNFRAMELISTBOX1,
              name='listBox1', parent=self, pos=wx.Point(24, 344),
              size=wx.Size(496, 88), style=0)
        self.listBox1.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListBox1ListboxDclick,
              id=wxID_CADFORNFRAMELISTBOX1)

        self.staticText5 = wx.StaticText(id=wxID_CADFORNFRAMESTATICTEXT5,
              label=u'Codigo', name='staticText5', parent=self, pos=wx.Point(24,
              328), size=wx.Size(34, 13), style=0)

        self.staticText6 = wx.StaticText(id=wxID_CADFORNFRAMESTATICTEXT6,
              label=u'Nome', name='staticText6', parent=self, pos=wx.Point(120,
              328), size=wx.Size(28, 13), style=0)

        self.staticText7 = wx.StaticText(id=wxID_CADFORNFRAMESTATICTEXT7,
              label=u'Telefone', name='staticText7', parent=self,
              pos=wx.Point(416, 328), size=wx.Size(43, 13), style=0)

    def __init__(self, parent):

        self._init_ctrls(parent)
        

    def OnNovoBtnButton(self, event):
        self.alterar_campos(True)
        self.textCtrl1.SetValue('')
        self.textCtrl2.SetValue('')
        self.textCtrl3.SetValue('')


    def alterar_campos(self, VF):

        self.textCtrl3.SetEditable(VF)
        self.textCtrl3.Enable(VF)
        self.textCtrl2.SetEditable(VF)
        self.textCtrl2.Enable(VF)


        
        
    def OnAlterarBtnButton(self, event):
        self.alterar_campos(True)

    def OnSalvarBtnButton(self, event):
        c = Controle()
        codigo = self.textCtrl1.GetValue()
        nome = self.textCtrl2.GetValue()
        telefone = self.textCtrl3.GetValue()
        
        atributos = [nome, telefone, codigo]
        
        validacao = c.salvar_fornecedor(atributos)
        
        if validacao == True:
            self.OnNovoBtnButton
            self.alterar_campos(False)
            
            m = 'Fornecedor Salvo com Sucesso'
            c = 'Cadastro de Fornecedor'
            wx.MessageBox(m,c,wx.OK)
         
        

    def OnCancelarBtnButton(self, event):
        self.Close()

    def OnExcluirBtnButton(self, event):
        cod = self.textCtrl1.GetValue()
        c = Controle()
        validacao = c.remover_fornecedor(cod)
        if validacao == True:
            self.alterar_campos(False)
            self.OnLimparBtnButton(event)
            m = 'Fornecedor Excluido com Sucesso'
            c = 'Cadastro de Fornecedor'
            wx.MessageBox(m,c,wx.OK)
            

    def OnPesquisaBtnButton(self, event):
        self.listBox1.Clear()
        nome = self.txtPesquisa.GetValue()
        c = Controle()
        lista = c.procurar_fornecedor_nome(nome)
        
        def codd(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x
            
        def desc(x):
            x = str(x)
            while len(x)<80:
                x = x+' '
            return x
            
        def tel(x):
            x = str(x)
            while len(x)<20:
                x = x+' '
            return x

        for r in lista:
            addd = codd(+r[0])+desc(r[1])+tel(r[2])
            self.listBox1.Append(addd)
                   
    def OnLimparBtnButton(self, event):
        self.txtPesquisa.SetValue('')
        self.listBox1.Clear()

    def OnListBox1ListboxDclick(self, event):
        a = self.listBox1.GetStringSelection()
        cod = a[0:2]
        cod = cod.split(" ")
        cod = str(cod[0])
        
        c = Controle()
        atributos = c.procurar_fornecedor_codigo(cod)
        
        self.textCtrl1.SetValue(str(atributos[0]))
        self.textCtrl2.SetValue(str(atributos[1]))
        self.textCtrl3.SetValue(str(atributos[2]))
        
        
        self.alterar_campos(False)
        
